package com.mingshashan.building.easy.core.controller;

import com.mingshashan.building.easy.core.common.EasyPage;
import com.mingshashan.building.easy.core.common.PageRequest;
import com.mingshashan.building.easy.core.service.IPositionService;
import com.mingshashan.building.easy.core.vo.PositionCreationVO;
import com.mingshashan.building.easy.core.vo.PositionUpdateVO;
import com.mingshashan.building.easy.core.vo.PositionVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * PositionController
 *
 * @author jasonxu
 */
@Api(value = "岗位管理", tags = {"岗位管理"})
@RestController
@RequestMapping("/positions")
public class PositionController {

    @Autowired
    private IPositionService positionService;

    @ApiOperation("add position")
    @PostMapping()
    public PositionVO add(@RequestBody PositionCreationVO positionCreationVO) {
        return positionService.add(positionCreationVO);
    }

    @ApiOperation("delete position")
    @DeleteMapping("{id}")
    public ResponseEntity<Void> delete(@PathVariable("id") long id) {
        positionService.delete(id);

        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @ApiOperation("update position")
    @PutMapping("{id}")
    public PositionVO update(@PathVariable("id") long id,
                             @RequestBody PositionUpdateVO positionUpdateVO) {
        return positionService.update(id, positionUpdateVO);
    }

    @ApiOperation("get position by id")
    @GetMapping("{id}")
    public PositionVO getById(@PathVariable("id") long id) {
        return positionService.getById(id);
    }

    @ApiOperation("get position by position code")
    @GetMapping()
    public PositionVO getByCode(@RequestParam("code") String code) {
        return positionService.getByCode(code);
    }

    @ApiOperation("search position")
    @GetMapping("/query")
    public EasyPage<PositionVO> searchPositions(@RequestParam(name = "code", required = false) String code,
                                                @RequestParam(name = "name", required = false) String name,
                                                @RequestParam(name = "page", required = false, defaultValue = "0") int page,
                                                @RequestParam(name = "size", required = false, defaultValue = "10") int size) {


        PageRequest pageRequest = new PageRequest(page, size);
        return positionService.searchPositions(code, name, pageRequest);
    }

}
